package com.xyz.controller;
import com.xyz.aop.ObjectParam;
import com.xyz.aop.Param;
import com.xyz.dto.EquipmentDto;
import com.xyz.dto.EquipmentElectricalDto;
import com.xyz.entity.mapper.EquipmentTreeMapper;
import com.xyz.entity.pojo.EquipmentTree;
import com.xyz.entity.pojo.EquipmentTreeExample;
import com.xyz.entity.vo.EquipmentInfoExportVo;
import com.xyz.service.CommonService;
import com.xyz.service.EquipmentService;
import com.xyz.service.EquipmentTreeService;
import com.xyz.util.BeanUtils;
import com.xyz.util.ExcelUtils;
import com.xyz.util.dto.DataResult;
import com.xyz.util.dto.GridData;
import com.xyz.util.dto.LoginInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("equipment")
public class EquipmentController {

    private static final Logger logger = LoggerFactory.getLogger(EquipmentController.class);

    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private EquipmentTreeMapper equipmentTreeMapper;
    @Autowired
    private EquipmentTreeService equipmentTreeService;

    @PostMapping("selectEquipmentTree")
    public DataResult selectEquipmentTree(
            LoginInfo li
    ) {
        try {
            return equipmentService.selectEquipmentTree(li);
        } catch (Exception e) {
            logger.error("selectEquipmentTree", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectEquipmentTreeGroup")
    public DataResult selectEquipmentTreeGroup(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.selectEquipmentTreeGroup(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectEquipmentTreeGroup", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectEquipmentTreeGroupNoEquipment")
    public DataResult selectEquipmentTreeGroupNoEquipment(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.selectEquipmentTreeGroupNoEquipment(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectEquipmentTreeGroupNoEquipment", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updateEquipmentTreeGroup")
    public DataResult updateEquipmentTreeGroup(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String type,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String baseTreeId
    ) {
        try {
            return equipmentService.updateEquipmentTreeGroup(li, Integer.parseInt(type), Long.parseLong(id), Long.parseLong(baseTreeId));
        } catch (Exception e) {
            logger.error("updateEquipmentTreeGroup", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("insertEquipmentTreeGroupInfo")
    public DataResult insertEquipmentTreeGroupInfo(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String baseId,
            @Param String name,
            @Param String code,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String emType
    ) {
        try {
            return equipmentService.insertEquipmentTreeGroupInfo(li, Long.parseLong(baseId), name, code, Integer.parseInt(emType));
        } catch (Exception e) {
            logger.error("insertEquipmentTreeGroupInfo", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updateEquipmentTreeGroupInfo")
    public DataResult updateEquipmentTreeGroupInfo(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param String name,
            @Param String code,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String emType
    ) {
        try {
            return equipmentService.updateEquipmentTreeGroupInfo(li, Long.parseLong(id), name, code, Integer.parseInt(emType));
        } catch (Exception e) {
            logger.error("updateEquipmentTreeGroupInfo", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("deleteEquipmentTreeGroupInfo")
    public DataResult deleteEquipmentTreeGroupInfo(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.deleteEquipmentTreeGroupInfo(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("deleteEquipmentTreeGroupInfo", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectEquipment")
    public DataResult selectEquipment(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(type = Param.ParamType.POSITIVE_INTEGER, isRequired = false) String subsystemId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER, isRequired = false) String emGrade,
            @Param(type = Param.ParamType.POSITIVE_INTEGER, isRequired = false) String emState,
            @Param(type = Param.ParamType.POSITIVE_INTEGER, isRequired = false) String subTreeId,
            @Param(isRequired = false) String emName
    ) {
        try {
            return equipmentService.selectEquipment(li, Integer.parseInt(pageNum), Integer.parseInt(pageSize), subsystemId, emGrade, emState, subTreeId,emName);
        } catch (Exception e) {
            logger.error("selectEquipment", e);
            return DataResult.build9500();
        }
    }

    /*导出设备台账列表*/
    @GetMapping(value ="exportData")
    public DataResult exportData(LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(type = Param.ParamType.POSITIVE_INTEGER, isRequired = false) String subsystemId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER, isRequired = false) String emGrade,
            @Param(type = Param.ParamType.POSITIVE_INTEGER, isRequired = false) String emState,
            @Param(type = Param.ParamType.POSITIVE_INTEGER, isRequired = false) String subTreeId,
            @Param(isRequired = false) String emName,
            HttpServletResponse response) throws Exception {
        try {
            DataResult result = equipmentService.selectEquipment(li, Integer.parseInt(pageNum), Integer.parseInt(pageSize),
                    subsystemId, emGrade, emState, subTreeId, emName);
            Object obj = result.getInfo();
            List<EquipmentInfoExportVo> personList = new ArrayList<>();
            if (obj instanceof GridData) {
                GridData gridData = (GridData) obj;
                personList = BeanUtils.toBean(gridData.getRows(), EquipmentInfoExportVo.class);
                //设备级别
                Map<Integer, String> emGradeMap = commonService.getDictMap("TYPE_EM_GRADE");
                //设备类别
                Map<Integer, String> emTypeMap = commonService.getDictMap("TYPE_EM_TYPE");
                //设备状态
                Map<Integer, String> emStateMap = commonService.getDictMap("TYPE_EM_STATE");
                EquipmentTreeExample etExample = new EquipmentTreeExample();
                etExample.createCriteria().andIsDeleteEqualTo(0);
                List<EquipmentTree> etList = equipmentTreeMapper.selectByExample(etExample);

                for (EquipmentInfoExportVo equipmentInfoExportVo : personList) {
                    equipmentInfoExportVo.setEmGradeName(emGradeMap.get(equipmentInfoExportVo.getEmGrade()));
                    equipmentInfoExportVo.setEmTypeName(emTypeMap.get(equipmentInfoExportVo.getEmType()));
                    equipmentInfoExportVo.setEmStateName(emStateMap.get(equipmentInfoExportVo.getEmState()));
                    if (equipmentInfoExportVo.getIsSpecial() == 0){
                        equipmentInfoExportVo.setIsSpecialName("否");
                    }else {
                        equipmentInfoExportVo.setIsSpecialName("是");
                    }
                    if (equipmentInfoExportVo.getIsScrap() == 0){
                        equipmentInfoExportVo.setIsScrapName("否");
                    }else {
                        equipmentInfoExportVo.setIsScrapName("是");
                    }
                    if (equipmentInfoExportVo.getIsProvide() == 0){
                        equipmentInfoExportVo.setIsProvideName("否");
                    }else {
                        equipmentInfoExportVo.setIsProvideName("是");
                    }
                    if (equipmentInfoExportVo.getIsElectrical() == 0){
                        equipmentInfoExportVo.setIsElectricalName("否");
                    }else {
                        equipmentInfoExportVo.setIsElectricalName("是");
                    }
                    List<String> nodeStr = equipmentTreeService.reverseSelectNode(etList, equipmentInfoExportVo.getEmTreeId());
                    equipmentInfoExportVo.setElectricalClass(String.join("/", nodeStr));
                }
            }
            ExcelUtils.write(response, "设备台账.xls", "设备台账", EquipmentInfoExportVo.class,
                    personList);
            return result;
        } catch (Exception e) {
            logger.error("exportData", e);
        }
        return DataResult.build9100();
    }

    @PostMapping("selectEquipmentInfo")
    public DataResult selectEquipmentInfo(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.selectEquipmentInfo(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectEquipmentInfo", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("insertEquipmentInfo")
    public DataResult insertEquipmentInfo(
            LoginInfo li,
            @ObjectParam EquipmentDto dto
    ) {
        try {
            return equipmentService.insertEquipmentInfo(li, dto);
        } catch (Exception e) {
            logger.error("insertEquipmentInfo", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updateEquipmentInfo")
    public DataResult updateEquipmentInfo(
            LoginInfo li,
            @ObjectParam EquipmentDto dto
    ) {
        try {
            return equipmentService.updateEquipmentInfo(li, dto);
        } catch (Exception e) {
            logger.error("updateEquipmentInfo", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updateEquipmentInfoOfScrap")
    public DataResult updateEquipmentInfoOfScrap(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.updateEquipmentInfoOfScrap(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("updateEquipmentInfoOfScrap", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectEquipmentData")
    public DataResult selectEquipmentData(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.selectEquipmentData(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectEquipmentData", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("deleteEquipmentInfo")
    public DataResult deleteEquipmentInfo(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.deleteEquipmentInfo(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("deleteEquipmentInfo", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updateEquipmentData")
    public DataResult updateEquipmentData(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(isRequired = false) String emDesc,
            @Param(isRequired = false) String emFileCodes
    ) {
        try {
            return equipmentService.updateEquipmentData(li, Long.parseLong(id), emDesc, emFileCodes);
        } catch (Exception e) {
            logger.error("updateEquipmentData", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectEquipmentDocument")
    public DataResult selectEquipmentDocument(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String type
    ) {
        try {
            return equipmentService.selectEquipmentDocument(li, Integer.parseInt(pageNum), Integer.parseInt(pageSize), Long.parseLong(id), Integer.parseInt(type));
        } catch (Exception e) {
            logger.error("selectEquipmentDocument", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("insertEquipmentDocument")
    public DataResult insertEquipmentDocument(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String type,
            @Param String fileCodes
    ) {
        try {
            return equipmentService.insertEquipmentDocument(li, Long.parseLong(id), Integer.parseInt(type), fileCodes);
        } catch (Exception e) {
            logger.error("insertEquipmentDocument", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("deleteEquipmentDocument")
    public DataResult deleteEquipmentDocument(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.deleteEquipmentDocument(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("deleteEquipmentDocument", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectEquipmentOperationLog")
    public DataResult selectEquipmentOperationLog(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.selectEquipmentOperationLog(li, Integer.parseInt(pageNum), Integer.parseInt(pageSize), Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectEquipmentOperationLog", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectEquipmentMoveLog")
    public DataResult selectEquipmentMoveLog(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.selectEquipmentMoveLog(li, Integer.parseInt(pageNum), Integer.parseInt(pageSize), Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectEquipmentMoveLog", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectEquipmentPlc")
    public DataResult selectEquipmentPlc(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.selectEquipmentPlc(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectEquipmentPlc", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("insertEquipmentPlc")
    public DataResult insertEquipmentPlc(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String emId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String type,
            @Param String unit,
            @Param String upperLimit,
            @Param String lowerLimit
    ) {
        try {
            return equipmentService.insertEquipmentPlc(li, Long.parseLong(emId), Integer.parseInt(type), unit, upperLimit, lowerLimit);
        } catch (Exception e) {
            logger.error("insertEquipmentPlc", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updateEquipmentPlc")
    public DataResult updateEquipmentPlc(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String type,
            @Param String unit,
            @Param String upperLimit,
            @Param String lowerLimit
    ) {
        try {
            return equipmentService.updateEquipmentPlc(li, Long.parseLong(id), Integer.parseInt(type), unit, upperLimit, lowerLimit);
        } catch (Exception e) {
            logger.error("updateEquipmentPlc", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("deleteEquipmentPlc")
    public DataResult deleteEquipmentPlc(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.deleteEquipmentPlc(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("deleteEquipmentPlc", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectEquipmentUpkeep")
    public DataResult selectEquipmentUpkeep(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.selectEquipmentUpkeep(li, Integer.parseInt(pageNum), Integer.parseInt(pageSize), Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectEquipmentUpkeep", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectEquipmentPatrol")
    public DataResult selectEquipmentPatrol(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.selectEquipmentPatrol(li, Integer.parseInt(pageNum), Integer.parseInt(pageSize), Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectEquipmentPatrol", e);
            return DataResult.build9500();
        }
    }


    @PostMapping("selectEquipmentPatrolByEmId")
    public DataResult selectEquipmentPatrolByEmId(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.selectEquipmentPatrolByEmId(li, Integer.parseInt(pageNum), Integer.parseInt(pageSize), Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectEquipmentPatrol", e);
            return DataResult.build9500();
        }
    }


    @PostMapping("selectEquipmentAuxiliary")
    public DataResult selectEquipmentAuxiliary(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.selectEquipmentAuxiliary(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectEquipmentAuxiliary", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("insertEquipmentAuxiliary")
    public DataResult insertEquipmentAuxiliary(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param String fileCode
    ) {
        try {
            return equipmentService.insertEquipmentAuxiliary(li, Long.parseLong(id), fileCode);
        } catch (Exception e) {
            logger.error("insertEquipmentAuxiliary", e);
            return DataResult.build9500();
        }
    }

    /*
     * 维保计划配置树
     * */
    @PostMapping("selectEquipmentTreeUpkeep")
    public DataResult selectEquipmentTreeUpkeep(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.selectEquipmentTreeUpkeep(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectEquipmentTreeUpkeep", e);
            return DataResult.build9500();
        }
    }

    /*
     * 查询电器元件列表
     * */
    @PostMapping("selectEquipmentElectrical")
    public DataResult selectEquipmentElectrical(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.selectEquipmentElectrical(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectEquipmentElectrical", e);
            return DataResult.build9500();
        }
    }

    /*
     * 新增电器元件列表
     * */
    @PostMapping("insertEquipmentElectrical")
    public DataResult insertEquipmentElectrical(
            LoginInfo li,
            @ObjectParam EquipmentElectricalDto equipmentElectricalDto
    ) {
        try {
            return equipmentService.insertEquipmentElectrical(li, equipmentElectricalDto);
        } catch (Exception e) {
            logger.error("insertEquipmentElectrical", e);
            return DataResult.build9500();
        }
    }

    /*
     * 编辑电器元件列表
     * */
    @PostMapping("updateEquipmentElectrical")
    public DataResult updateEquipmentElectrical(
            LoginInfo li,
            @ObjectParam EquipmentElectricalDto equipmentElectricalDto
    ) {
        try {
            return equipmentService.updateEquipmentElectrical(li, equipmentElectricalDto);
        } catch (Exception e) {
            logger.error("updateEquipmentElectrical", e);
            return DataResult.build9500();
        }
    }

    /*
     * 编辑电器元件列表
     * */
    @PostMapping("deleteEquipmentElectrical")
    public DataResult deleteEquipmentElectrical(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return equipmentService.deleteEquipmentElectrical(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("deleteEquipmentElectrical", e);
            return DataResult.build9500();
        }
    }


    @PostMapping("details")
    public DataResult selectDetails(LoginInfo li, Long id
    ) {
        try {
            return equipmentService.selectDetails(id);
        } catch (Exception e) {
            logger.error("selectDetails", e);
            return DataResult.build9500();
        }
    }
}
